package com.gxa.user.controller.user;

import com.gxa.o2o.core.dto.ResultDTO;

import com.gxa.o2o.core.exception.BusinessException;

import com.gxa.user.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 阔爱又迷人的川锅锅呀
 */
@RestController
@RequestMapping("/user")
@Api("用户端相关接口")
@ApiResponses({
        @ApiResponse(code = 400, message = "请求参数不完整或者错误"),
        @ApiResponse(code = 404, message = "找不到页面"),
        @ApiResponse(code = 405, message = "请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code = 406, message = "页面地址和接口名冲突"),
        @ApiResponse(code = 415, message = "请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code = 500, message = "后台服务逻辑错误")
})
public class UserController {
    @Autowired

    @Resource(name = "userService")
    private UserService userService;

    /**
     * 第三方Gitee登录接口
     * @return
     */
    @GetMapping("/loginByGit")
    @ApiOperation(value = "第三方Git登录接口",notes = "必须是网页请求",httpMethod = "GET")
    public void gitUserLogin(HttpServletResponse response) throws IOException {
        userService.loginByGit(response);
    }

    /**
     * 第三方登录回调接口
     * @param code
     * @return
     */
    @GetMapping ("/authorization_code")
    @ApiOperation(value = "第三方登录服务回调接口",notes = "第三方服务回调使用",httpMethod = "GET")
    public ResultDTO authorizationCode(String code,HttpServletRequest request) throws BusinessException {
       return userService.callBack(code,request);
    }

    @PostMapping("/loginByPhone")
    @ApiOperation(value = "用户手机号一键登录接口",notes = "登录",httpMethod = "POST")
    public ResultDTO loginByPhone(@RequestParam("phone") String phone, @RequestParam("code") String code){
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            return new ResultDTO(5003,"手机号码格式不合法");
        }
        return userService.loginByPhone(phone,code);
    }

    /**
     * 获取手机验证码
     * @param phone
     * @return
     * @throws BusinessException
     */
    @PostMapping("/getVerifyCode")
    @ApiOperation(value = "获取手机验证码接口",notes = "获取验证码",httpMethod = "POST")
    @ApiImplicitParam(name = "phone",value = "用户手机号")
    public ResultDTO getVerifyCode(@RequestParam("phone") String phone) throws BusinessException {
        return userService.getVerifyCode(phone);
    }
}
